/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.crowtheries.wirelessrouting.messages;

import ptolemy.data.IntToken;
import ptolemy.data.RecordToken;
import ptolemy.data.StringToken;
import ptolemy.data.Token;
import ptolemy.kernel.util.IllegalActionException;

/**
 * RREQ Class that generates the RREQ_Token
 * @author knuthy
 */
public class RREQ{
    private RecordToken rreqToken;

    public RREQ(String SRCIP, int SRCSEQ, String DSTIP, int DSTSEQ, int RREQID) throws IllegalActionException{
        rreqToken=new RecordToken(new String[]{
            "SRCIP",
            "SRCSEQ",
            "DSTIP",
            "DSTSEQ",
            "RREQID",
            "HOPCOUNT"
        },new Token[]{
            new StringToken(SRCIP),
            new IntToken(SRCSEQ),
            new StringToken(DSTIP),
            new IntToken(DSTSEQ),
            new IntToken(RREQID),
            new IntToken(0)
        });
    }

    public RREQ(RecordToken rreq){
        this.rreqToken=rreq;
    }

    public String getSRCIP(){
        return ((StringToken)rreqToken.get("SRCIP")).stringValue();
    }

    public String getDSTIP(){
        return ((StringToken)rreqToken.get("DSTIP")).stringValue();
    }

    public int getSRCSEQ(){
        return ((IntToken)rreqToken.get("SRCSEQ")).intValue();
    }

    public int getDSTSEQ(){
        return ((IntToken)rreqToken.get("DSTSEQ")).intValue();
    }

    public int getHOPCOUNT(){
        return ((IntToken)rreqToken.get("HOPCOUNT")).intValue();
    }

    public int getRREQID(){
        return ((IntToken)rreqToken.get("RREQID")).intValue();
    }

    /**
     * Increments the HOPCOUNT value
     * @throws ptolemy.kernel.util.IllegalActionException
     */
    public void incHop() throws IllegalActionException{
        IntToken hopCount= (IntToken) rreqToken.get("HOPCOUNT");
        rreqToken.merge(rreqToken, new RecordToken(new String[]{"HOPCOUNT"},
                new Token[]{new IntToken(hopCount.intValue()+1)}));
    }

    public RecordToken getMessage() throws IllegalActionException{
        return new RecordToken(new String[]{
            "MSGTYPE",
            "MSG"
        },new Token[]{
            new IntToken(1),
            rreqToken
        });
    }

    public RecordToken getRequestToken() throws IllegalActionException{
        RecordToken message=new RecordToken(new String[]{
            "MSGTYPE",
            "MSG"
        },new Token[]{
            new IntToken(1),
            rreqToken
        });

        RecordToken segUDP=new RecordToken(new String[]{
            "DSTP",
            "SRCP",
            "DATA"
        },new Token[]{
            new IntToken(654),
            new IntToken(654),
            message
        });

        RecordToken packet=new RecordToken(new String[]{
            "DSTIP",
            "SEG"
        },new Token[]{
            new StringToken("255.255.255"),
            segUDP
        });
        return packet;
    }

    public boolean equals(RREQ req) throws IllegalActionException{
        if(this.getSRCIP().equals(req.getSRCIP()) &&
                this.getRREQID()==req.getRREQID())
            return true;
        else
            return false;
    }
}
